package com.mszlu.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mszlu.news.dao.mapper.ArticleMapper;
import com.mszlu.news.dao.mapper.CollectMapper;
import com.mszlu.news.dao.pojo.Article;
import com.mszlu.news.dao.pojo.Collect;
import com.mszlu.news.service.CollectService;
import com.mszlu.news.service.SysUserService;
import com.mszlu.news.service.TagService;
import com.mszlu.news.vo.ArticleVo;
import com.mszlu.news.vo.CollectVo;
import com.mszlu.news.vo.Result;
import com.mszlu.news.vo.params.CollectParam;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private TagService tagService;
    @Autowired
    private SysUserService sysUserService;

    public Result listCollectByUserId(Long userId){
        List<ArticleVo> collectList=collectMapper.listCollectByUserId(userId);
        for (ArticleVo record : collectList){
            record.setTags(tagService.findTagsByArticleId(Long.valueOf(record.getId())));
            record.setAuthorId(String.valueOf(record.getAuthorId()));
            record.setAuthor(sysUserService.findUserById(Long.valueOf(record.getAuthorId())).getNickname());
            record.setAvatar(sysUserService.findUserById(Long.valueOf(record.getAuthorId())).getAvatar());
            record.setV(sysUserService.findUserById(Long.valueOf(record.getAuthorId())).getV());
        }
        return Result.success(collectList);
    }

    @Override
    public Result addCollect(CollectParam collectParam) {
        Collect collect=new Collect();
        collect.setArticleId(collectParam.getArticle_id());
        collect.setUserId(collectParam.getUser_id());
        collectMapper.insert(collect);
        Article article=articleMapper.selectById(collectParam.getArticle_id());
        int CollectCounts=article.getCollectCounts();
        article.setId(collectParam.getArticle_id());
        article.setCollectCounts(CollectCounts+1);//设置点赞数+1
        articleMapper.updateById(article);
        return Result.success("收藏添加成功");
    }

    @Override
    public Result deleteCollect(CollectParam collectParam) {
        Map<String,Object> columnMap = new HashMap<>();
        columnMap.put("article_id",collectParam.getArticle_id());
        columnMap.put("user_id",collectParam.getUser_id());
        collectMapper.deleteByMap(columnMap);
        Article article=articleMapper.selectById(collectParam.getArticle_id());
        int CollectCounts=article.getCollectCounts();
        article.setId(collectParam.getArticle_id());
        article.setGoodCounts(CollectCounts-1);//设置点赞数+1
        articleMapper.updateById(article);
        return Result.success("收藏删除成功");
    }

    @Override
    public Result CollectCounts(Long articleId) {
        QueryWrapper<Collect> query = new QueryWrapper<>();
        query.eq("article_id",articleId);
        int CollectCounts=collectMapper.selectCount(query);
        Article article=new Article();
        article.setId(articleId);
        article.setCollectCounts(CollectCounts);
        articleMapper.updateById(article);
        return Result.success(CollectCounts);
    }
}
